/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "mainwindow.h"

#include <sys/inotify.h>
#include <fcntl.h>
#include <syslog.h>

#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QStandardPaths>
#include <QLibraryInfo>

#include "log.hpp"

using namespace kdk::kabase;

int main(int argc, char *argv[])
{
    //高清屏幕自适应,适配990高DPI
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif

    qInstallMessageHandler(kdk::kabase::Log::logOutput);
    QApplication app(argc, argv);

    //检测单例，调用dbus接口
    QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
    int fd = open(QString(homePath.at(0) + "/.config/kylin-nw-checktool%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
                  O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd < 0) {
        exit(1);
    }
    if (lockf(fd, F_TLOCK, 0)) {
        syslog(LOG_ERR, "Can't lock single file, kylin-nw-checktool is already running!");
        exit(0);
    }

    app.setApplicationVersion("1.1.2.1");
    app.setWindowIcon(QIcon(":/data/Applogo.png"));
    //加载应用翻译
    QString appTranslationsPath;
    if (QDir("/usr/share/kylin-nw-checktool/translations").exists()) {
        appTranslationsPath = "/usr/share/kylin-nw-checktool/translations";
    } else {
        appTranslationsPath = app.applicationDirPath() + "../translations";
    }
    QString locale = QLocale::system().name();
    QTranslator trans_global;
    if (!trans_global.load("kylin-netcheck-tools_" + locale + ".qm", appTranslationsPath)) {
        qDebug() << "Load translations file" << QLocale() << "failed!";
    } else {
        app.installTranslator(&trans_global);
    }
    //加载QT的翻译
    QString qtTranslationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
    QTranslator qt_trans;
    if (!qt_trans.load("qt_" + locale + ".qm", qtTranslationsPath)) {
        qDebug() << "Load translation file："
                 << "qt_" + locale + ".qm from" << qtTranslationsPath << "failed!";
    } else {
        app.installTranslator(&qt_trans);
    }
    //加载SDK的翻译
    QTranslator sdk_trans;
    if (!sdk_trans.load(":/translations/gui_" + locale + ".qm")) {
        qDebug() << "Load sdk translation file failed!";
    } else {
        app.installTranslator(&sdk_trans);
    }

    qRegisterMetaType<CHECKRESULT>("CHECKRESULT");
    qRegisterMetaType<InnerNetCheck>("InnerNetCheck&");
    qRegisterMetaType<QMap<QString, bool>>("QMap<QString,bool>");
    MainWindow w;
    w.show();
    qDebug() << "=====================main thread:" << QThread::currentThreadId() << "=====================";
    return app.exec();
}
